/*
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 *
 * IPList
 * Idea and C# code by Bo Norgaard
 * C++ code by SSA
 *
 */


#include "stdinc.h"
#include <Winsock2.h>

#include "iplist.h"



IPList::IPList()
{
	// Initialize IP mask list and create IPArrayList into the ipRangeList
	UINT32 mask = 0x00000000;
	for(UINT32 level = 1; level<33; level++) {
		mask = (mask >> 1) | 0x80000000;
		maskList[ mask ] =  level ;
		ipRangeList.push_back( boost::shared_ptr<IPList::IPArrayList>(new IPList::IPArrayList (mask)) );
	}
}

IPList::~IPList()
{
	Clear();
}

UINT32 
IPList::parseIP(const std::string& IPNumber)
{
	UINT32 res = INADDR_NONE;
	res = inet_addr(IPNumber.c_str());
	if (res != INADDR_NONE)
		res = ntohl(res);
	return res;
}

void 
IPList::Add(const std::string& IPNumber)
{
	UINT32 ip = parseIP(IPNumber);
	if (ip != INADDR_NONE)
		Add(ip);
}

void 
IPList::Add(UINT32 ip)
{
	if (ip == INADDR_NONE)
		return;

	ipRangeList[31].get()->Add(ip);
	if (std::find( usedList.begin(), usedList.end(), (UINT32)32) == usedList.end())
	{
		usedList.push_back(31);
		std::sort(usedList.begin(), usedList.end());
	}
}

void 
IPList::Add(const std::string& IPNumber, const std::string& Mask)
{
	UINT32 ip = parseIP(IPNumber);
	UINT32 umask = parseIP(Mask);
	if (ip != INADDR_NONE && umask != INADDR_NONE)
		Add(ip, umask);
}

void 
IPList::Add(UINT32 ip, UINT32 umask)
{
	if (ip == INADDR_NONE && umask == INADDR_NONE)
		return;
	std::map<UINT32, UINT32>::const_iterator it = maskList.find(umask);
	if (it != maskList.end())
	{
		UINT32 Level = maskList[umask];
		if ( Level  ) {
			ip = ip & umask;
			ipRangeList[(int)Level-1].get()->Add(ip);
			if (std::find(usedList.begin(), usedList.end(), (UINT32)Level-1) == usedList.end())
			{
				usedList.push_back((UINT32)Level-1);
				std::sort(usedList.begin(), usedList.end());
			}
		}
	}
}

void 
IPList::Add(const std::string& IPNumber, UINT32 maskLevel)
{
	UINT32 ip = parseIP(IPNumber);
	UINT32 umask = getMaskByLevel (maskLevel);

	if (ip != INADDR_NONE && umask != INADDR_NONE)
		Add(ip, umask);
}

UINT32 
IPList::getMaskByLevel(UINT32 maskLevel)
{
	UINT32 umask = INADDR_NONE;
	for (std::map<UINT32, UINT32>::const_iterator it = maskList.cbegin(); it != maskList.cend(); ++it)
	{
		if (it->second == maskLevel)
		{
			umask = it->first;
			break;
		}
	}
	return umask;
}


void 
IPList::AddRange(const std::string& fromIP, const std::string& toIP)
{
	UINT32 ufromIP = parseIP(fromIP);
	UINT32 utoIP = parseIP(toIP);
	if (ufromIP != INADDR_NONE && utoIP != INADDR_NONE)
		AddRange(ufromIP, utoIP);
}

void 
IPList::AddRange(UINT32 fromIP, UINT32 toIP)
{
	if (fromIP>toIP) {
		UINT32 tempIP = fromIP;
		fromIP=toIP;
		toIP=tempIP;
	}
	if (fromIP==toIP) {
		Add(fromIP);
	} else {
		UINT32 diff = toIP-fromIP;
		int diffLevel = 1;
		UINT32 range = 0x80000000;
		if (diff<256) {
			diffLevel = 24;
			range = 0x00000100;
		}
		while (range>diff) {
			range = range>>1;
			diffLevel++;
		}

		UINT32 mask = getMaskByLevel(diffLevel);

		UINT32 minIP = fromIP & mask;
		if (minIP<fromIP) minIP+=range;
		if (minIP>fromIP) {
			AddRange(fromIP,minIP-1);
			fromIP=minIP;
		}
		if (fromIP==toIP) {
			Add(fromIP);
		} else {
			if ((minIP+(range-1))<=toIP) {
				Add(minIP,mask);
				fromIP = minIP+range;
			}
			if (fromIP==toIP) {
				Add(toIP);
			} else {
				if (fromIP<toIP) AddRange(fromIP,toIP);
			}
		}
	}
}

bool 
IPList::CheckNumber(const std::string& ipNumber)
{
	UINT32 ip = parseIP(ipNumber);
	if (ip != INADDR_NONE)
		return CheckNumber(ip);
	return false;
}


bool 
IPList::CheckNumber(UINT32 ip)
{
	bool found = false;
	size_t i=0;
	while (!found && i<usedList.size()) {
		found = ipRangeList[(int)usedList[i]].get()->Check(ip);
		i++;
	}
	return found;
}

void
IPList::Clear()
{

	for (size_t i = 0; i<usedList.size(); i++)
		ipRangeList[(int)usedList[i]].get()->Clear();
	usedList.clear();

}